package cn.mr;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class NewsReducer extends Reducer<Text, NewsBean, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<NewsBean> values, Context context)
            throws IOException, InterruptedException {
        long totalHeat = 0;
        // 统计各个类别的新闻数量,和总热度
        int allNews = 0;
        for (NewsBean newsBean : values) {
            totalHeat += newsBean.getHeat();
            allNews++;
        }
        //统计各个类别的平均热度
        double avgHeat = totalHeat / allNews;
        String resultStr = String.format("%d\t%.2f\t%d", allNews, avgHeat, totalHeat);
        context.write(key, new Text(resultStr));
    }
}
